<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <vxe-grid ref='repairOrderGrid' v-bind="gridOptions" class="my-grid66">
        <!--搜索表单 -->
        <template #form>
          <a-form
            :model="where"
            :label-col="{md: {span: 8}, sm: {span: 24}}"
            :wrapper-col="{md: {span: 16}, sm: {span: 24}}">
            <a-row>

<!--              <a-col :lg="6" :md="12" :sm="24"-->
<!--                     :xs="24">-->
<!--                <a-form-item label="学校:" name="school">-->
<!--                  <a-input-->
<!--                    v-model:value="where.school"-->
<!--                    :maxlength="255"-->
<!--                    placeholder="请输入学校"-->
<!--                    allow-clear-->
<!--                    class="ele-fluid"-->
<!--                  />-->
<!--                </a-form-item>-->
<!--              </a-col>-->
              <a-col :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="报修区域:" name="repairArea">
                  <m-dict-select
                    v-model:value="where.repairArea"
                    placeholder="请选择报修区域"
                    dict="报修区域"
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>
              <a-col :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="报修状态:" name="status">
                  <m-dict-select
                    v-model:value="where.status"
                    placeholder="请选择报修状态"
                    dict="维修单状态"
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>
<!--              <a-col :lg="6" :md="12" :sm="24"-->
<!--                     :xs="24">-->
<!--                <a-form-item label="详细位置:" name="detailedLocation">-->
<!--                  <a-input-->
<!--                    v-model:value="where.detailedLocation"-->
<!--                    :maxlength="255"-->
<!--                    placeholder="请输入详细位置"-->
<!--                    allow-clear-->
<!--                    class="ele-fluid"-->
<!--                  />-->
<!--                </a-form-item>-->
<!--              </a-col>-->
              <a-col  :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="故障类型:" name="faultType">
                  <a-input
                    v-model:value="where.faultType"
                    :maxlength="255"
                    placeholder="请输入故障类型"
                    allow-clear
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>

              <a-col v-if="searchExpand" :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="报修人:" name="repairApplicantId">
                  <m-entity-select
                    v-model:value="where.repairApplicantId"
                    placeholder="请选择报修人"
                    module="sys"
                    entity="user"
                    :query-filters="{userType:'user'}"
                    system="1"
                    show-options-name="nickname"
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>
              <a-col v-if="searchExpand" :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="报修电话:" name="repairApplicantPhone">
                  <a-input
                    v-model:value="where.repairApplicantPhone"
                    :maxlength="255"
                    placeholder="请输入报修电话"
                    allow-clear
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>
              <a-col v-if="searchExpand" :lg="6" :md="12" :sm="24"
                     :xs="24">
                <a-form-item label="维修级别:" name="repairLevel">
                  <m-dict-select
                    v-model:value="where.repairLevel"
                    placeholder="请选择维修级别"
                    dict="维修级别"
                    class="ele-fluid"
                  />
                </a-form-item>
              </a-col>

              <a-col :lg="6" :md="12" :sm="24" :xs="24">
                <a-form-item class="ele-text-right" :wrapper-col="{span: 24}">
                  <a-space>
                    <a-button type="primary" @click="reload">查询</a-button>
                    <a-button @click="reset">重置</a-button>
                    <a @click="searchExpand = !searchExpand">
                      <span v-if="searchExpand">收起 <up-outlined class="ele-text-small"/></span>
                      <span v-else>展开 <down-outlined class="ele-text-small"/></span>
                    </a>
                  </a-space>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </template>

        <template #operate="{ row }">
          <a-space>
            <a @click="editEvent(row)">修改</a>
            <a-divider type="vertical"/>
            <a class="ele-text-success" v-if="row.currentState==0" @click="dispatchShow(row.eid)">派单</a>
            <a-divider type="vertical"/>
            <a class="ele-text-warning" @click="viewEvent(row)">查看</a>
            <a-divider type="vertical"/>
            <a-popconfirm
              title="确定要删除此记录吗？"
              @confirm="deleteEvent(row)">
              <a class="ele-text-danger">删除</a>
            </a-popconfirm>
          </a-space>
        </template>


        <!--TODO:自定义列模板-->
        <template #dddd="{  }">
          <a href="http://showdoc.cqcflq.com/">文档地址</a>
        </template>

        <template #ycs>
          <a-space>
            <a-button type="primary" @click="addEvent()">
              <template #icon>
                <plus-outlined/>
              </template>
              <span>新建</span>
            </a-button>
            <!--            <a-button type="primary" @click="addEvent2()">-->
            <!--              <template #icon>-->
            <!--                <plus-outlined/>-->
            <!--              </template>-->
            <!--              <span>新建独立页</span>-->
            <!--            </a-button>-->
            <a-button type="danger" @click="batchDeleteEvent()">
              <template #icon>
                <delete-outlined/>
              </template>
              <span>批量删除</span>
            </a-button>
            <a-button @click="gridOptions.sortConfig.multiple=!gridOptions.sortConfig.multiple">
              <template #icon>
                <upload-outlined/>
              </template>
              <span>{{ gridOptions.sortConfig.multiple === false ? '单' : '多' }}字段排序</span>
            </a-button>

            <div>自动派单</div>
            <a-switch v-model:checked="autoEable" checked-children="开" un-checked-children="关" @change="autoChange"/>
          </a-space>
        </template>
      </vxe-grid>

      <!--编辑弹窗-->
      <vxe-modal v-model="repairOrderListApp.editModalShowing" ref="repairOrderEditModal"
                 :title="gridOptions.showEditTitle"
                 id="repairOrderEditModal"
                 width="800" height="400" min-width="460" min-height="320" :lock-view="false" :mask="false"
                 show-footer show-zoom resize remember storage transfer dblclickZoom fullscreen
                 :destroy-on-close="true"
      >
        <template #default>
          <div id="repairOrderEditModalContent">
          </div>
        </template>
        <template #footer>
          <div id="repairOrderEditModalFooter">
          </div>
          <teleport to="#repairOrderEditModalContent">
            <repairOrder-edit ref="repairOrder-edit"/>
          </teleport>
        </template>
      </vxe-modal>

      <!--派单弹窗-->
      <vxe-modal v-model="repairOrderListApp.dispatchModal" ref="repairOrderEditModal"
                 :title="gridOptions.showDispatchTitle"
                 id="repairOrderEditModal"
                 width="400" height="200" min-width="200" min-height="200" :lock-view="false" :mask="false"
                 show-zoom resize remember storage transfer dblclickZoom
                 :destroy-on-close="true"
                 :showFooter="false"
      >
        <a-form
          ref="dispatchForm"
          :model="dispatchData"
          name="basic"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 16 }"
          @finish="dispatchEvent"
          @finishFailed="onFinishFailed"
        >
          <a-form-item
            label="维修员"
            name="userId"
            :rules="[{ required: true, message: '请选择维修员!' }]"
          >
            <m-entity-select v-model:value="dispatchData.userId" system="1" module="sys" entity="user/listAllRepair"
                             show-options-name="nickname"></m-entity-select>
          </a-form-item>
          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" html-type="submit">提交</a-button>
          </a-form-item>
        </a-form>
        <template #footer>
        </template>
      </vxe-modal>

      <!--查看弹窗-->
      <vxe-modal v-model="repairOrderListApp.viewModalShowing" :title="gridOptions.showDetailsTitle"
                 id="repairOrderViewModal"
                 width="600"
                 min-width="460"
                 min-height="320"
                 :destroy-on-close="true"
                 :lock-view="false" :mask="false"
                 footer
                 show-footer show-zoom resize remember storage transfer dblclickZoom fullscreen>
        <template #default>
          <repairOrder-detail ref="repairOrder-detail"/>
        </template>
      </vxe-modal>
    </a-card>
  </div>
</template>

<script>
import {defineComponent, reactive, ref, toRefs, provide} from 'vue'//, Ref
import {VXETable} from 'vxe-table'//, VxeGridInstance, VxeGridProps
import XEAjax from 'xe-ajax'
import {RepairOrderService} from "@/views/repair/repairManagement/repairOrder/repairOrderService";
import RepairOrderEdit
  from "@/views/repair/repairManagement/repairOrder/repairOrder-edit";
import RepairOrderDetail
  from "@/views/repair/repairManagement/repairOrder/repairOrder-detail";
import {
  PlusOutlined,
  DeleteOutlined,
  DownOutlined,
  UpOutlined,
} from '@ant-design/icons-vue';
import {useRouter} from "vue-router";

import MDictSelect from "@/components/MDict/dictSelect";
import MEntitySelect from "@/components/MEntity/entitySelect";
// import mtUtil from "@/utils/mtUtil";
export default defineComponent({
  components: {
    RepairOrderEdit,
    RepairOrderDetail,
    DownOutlined,
    DeleteOutlined,
    PlusOutlined,
    UpOutlined,
    MDictSelect,
    MEntitySelect,
  },
  setup() {
    const router = useRouter();
    const repairOrderGrid = ref({})// as VxeGridInstance
    const dispatchForm = ref(null);
    // 搜索表单是否展开
    let searchExpand = ref(false)
    let autoEable = ref(true)
    let proxyInfo = reactive({})
    const where = ref({})
    const eid = ref({})
    const startIndex = ref({})
    const repairOrderListApp = reactive({
      editModalShowing: false,
      viewModalShowing: false,
      editModalForEdit: false,
      addModalForEdit: false,
      dispatchModal: false
    })
    const gridOptions = reactive({
      border: true,
      resizable: true,
      showHeaderOverflow: true,
      showOverflow: true,
      highlightHoverRow: true,
      keepSource: true,
      id: 'repairOrdersGrid',
      // maxHeight: "100%",//表格高度
      height: 800,
      // rowId: 'eid',
      selectRow: null,
      showEdit: false,
      showDetails: false,
      showEditTitle: '新建',
      showDetailsTitle: '查看',
      showDispatchTitle: "派单",
      formData: {},//自定义的
      detailData: [],
      startIndex: startIndex,
      printConfig: {
        columns: [
          {field: 'name'},
          {field: 'email'},
          {field: 'nickname'},
          {field: 'age'},
          {field: 'amount'}
        ]
      },
      /*TODO:排序配置 */
      sortConfig: {
        trigger: 'cell',
        remote: true,
        multiple: true,//多字段排序，默认单排序
        showIcon: true,
        // iconAsc: "fa fa-arrow-up fa-1x",
        // iconDesc: "fa fa-arrow-down fa-1x",
      },
      customConfig: {
        storage: true
      },
      /*TODO:过滤器配置*/
      filterConfig: {
        remote: true
      },
      /*TODO:分页配置*/
      pagerConfig: {
        pageSize: 10,
        pageSizes: [5, 10, 15, 20, 50]
      },
      /*TODO:工具栏配置*/
      toolbarConfig: {
        slots: {buttons: 'ycs'},
        refresh: true,
        import: true,
        export: true,
        zoom: true,
        custom: true
      },
      /*TODO:请求代理配置*/
      proxyConfig: {
        seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
        sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
        filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
        form: false, // 启用表单代理，当点击表单提交按钮时会自动触发 reload 行为
        // 对应响应结果 { result: [], page: { total: 100 } }
        props: {
          result: 'data.datas', // 配置响应结果列表字段
          total: 'data.totalCount', // 配置响应结果总页数字段
          message: 'data.msg'
        },
        /*TODO:请求*/
        ajax: {
          /*TODO:分页条件查询*/
          query: ({page, sorts, filters}) => {  // 接收 Promise

            const conditions = {...where.value}

            // 处理筛选条件
            filters.forEach(({property, values}) => {
              conditions[property] = values.join('$|$')
            })
            const queryParams = {}
            queryParams.currentPage = page.currentPage
            queryParams.pageSize = page.pageSize
            startIndex.value = (queryParams.currentPage - 1) * queryParams.pageSize + 1
            // // 处理筛选条件
            queryParams.filters = conditions;
            // // 处理排序条件
            queryParams.orderBys = sorts;
            queryParams.orderBys.push(
              {
                property: "createDatetime",
                order: "desc"
              }
            )
            //
            const $grid = repairOrderGrid.value
            $grid.queryParams = queryParams
            return RepairOrderService.findRepairOrders(queryParams).catch(error => {
              VXETable.modal.message({content: `加载数据出错，原因是：${error.message}`, status: 'error'});

            })
          },
        }
      },

      /*TODO:数据表列*/
      columns: [
        /*多选checkbox 单选seq*/
        {type: 'seq', title: '#', width: 40},
        // {, field: 'eid', title: 'ID', width: 100, sortable: true},
        {
          // type: 'checkbox',
          field: 'repairCode',
          title: '编号',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'repairApplicantName',
          title: '报修人',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'repairApplicantPhone',
          title: '电话',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'deviceName',
          title: '设备名称',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'deviceType',
          title: '设备类型',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'status',
          title: '状态',
          sortable: true,
          minWidth: 120
        },
        {
          field: 'school',
          title: '学校',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'repairArea',
          title: '报修区域',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'detailedLocation',
          title: '详细位置',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'faultType',
          title: '故障类型',
          sortable: true,
          minWidth: 80,
        },
        {
          field: 'expectRepairTime',
          title: '预期上门维修时间',
          sortable: true,
          minWidth: 140,
        },
        {
          field: 'repairTime',
          title: '上门维修时间',
          sortable: true,
          minWidth: 140,
        },
        {
          field: 'remark',
          title: '故障描述',
          sortable: true,
          minWidth: 120,
        },
        {
          field: 'repairLevel',
          title: '维修级别',
          sortable: true,
          minWidth: 120,
        },
        {title: '操作', width: 250, slots: {default: 'operate'}, fixed: "right"}
      ],
      /*TODO:导入配置*/
      importConfig: {
        remote: true,
        types: ['xlsx'],
        modes: ['insert'],
        // 自定义服务端导入
        importMethod({file}) {
          const $grid = repairOrderGrid.value
          const formBody = new FormData()
          formBody.append('file', file)
          return XEAjax.post('https://api.xuliangzhan.com:10443/demo/api/pub/import', formBody).then(data => {
            VXETable.modal.message({content: `成功导入 ${data.result.insertRows} 条记录！`, status: 'success'})
            // 导入完成，刷新表格
            $grid.commitProxy('query')
          }).catch(() => {
            VXETable.modal.message({content: '导入失败，请检查数据是否正确！', status: 'error'})
          })
        }
      },
      /*TODO:导出配置*/
      exportConfig: {
        remote: true,
        types: ['xlsx', 'csv', 'html', 'txt', 'pdf'],
        modes: ['current', 'selected', 'all'],
        // 自定义服务端导出
        exportMethod({options}) {
          const $grid = repairOrderGrid.value
          const proxyInfo = $grid.getProxyInfo()
          const queryParams = $grid.queryParams;
          // 传给服务端的参数
          const body = {
            ...queryParams,
            filename: options.filename,
            sheetName: options.sheetName,
            isHeader: options.isHeader,
            original1: options.original,
            mode: options.mode,
            pager: proxyInfo ? proxyInfo.pager : null,
            ids: options.mode === 'selected' ? options.data.map((item) => item.id) : [],
            fields: '%7B%22field%22:%22nickname%22,%22title%22:%22Nickname%22%7D&fields[]=%7B%22field%22:%22sex%22ge%22,%22title%22:%22Age%22%7D&fimount%22,%22title%22:%22Amount%22%7D&fields[]=%7B%22field'
          }

          // 开始服务端导出
          return RepairOrderService.findRepairOrdersForExport(body).then(data => {
            VXETable.modal.message({content: `成功${data}`, status: 'error'});
            // if (data.id) {
            // VXETable.modal.message({ content: '导出成功，开始下载', status: 'success' })
            // // 读取路径，请求文件
            // fetch(`https://api.xuliangzhan.com:10443/demo/api/pub/export/download/\${data.id}`).then(response => {
            //   response.blob().then(blob => {
            //     // 开始下载
            //     VXETable.saveFile({ filename: '导出数据', type: 'xlsx', content: blob })
            //   })
            // })
            // }
          }).catch(error => {
            VXETable.modal.message({content: `导出失败，原因是：${error.message}`, status: 'error'});

          })
        }


      },
      /*TODO:复选框配置*/
      checkboxConfig: {
        labelField: 'eid',
        reserve: false,
        highlight: false,
        range: true
      },
      /*TODO:编辑验证*/
      editRules: {
        name: [
          {required: true, message: 'app.body.valid.rName'},
          {min: 3, max: 50, message: '名称长度在 3 到 50 个字符'}
        ],
        email: [
          {required: true, message: '邮件必须填写'}
        ],
        role: [
          {required: true, message: '角色必须填写'}
        ]
      },
      editConfig: {
        trigger: 'click',
        mode: 'row',
        showStatus: true
      }
    })//as VxeGridProps
    provide('repairOrderListApp', repairOrderListApp)
    provide('gridOptions', gridOptions)
    provide('repairOrderGrid', repairOrderGrid)
    /*TODO:新建事件*/
    const addEvent = () => {
      repairOrderListApp.editModalShowing = true
      repairOrderListApp.currentId = null;
      repairOrderListApp.addModalForEdit = true;
      gridOptions.showEditTitle = "新建";
    }

    /*TODO:新建独立页*/
    const addEvent2 = () => {
      router.push(`/repair/repairManagement/repairOrder/repairOrder-add`);
    }
    /*TODO:编辑事件*/
    const editEvent = (row) => {
      repairOrderListApp.editModalShowing = true
      repairOrderListApp.currentId = row.eid;
      repairOrderListApp.editModalForEdit = true;
      gridOptions.showEditTitle = "编辑";
    }
    /*TODO:编辑独立页*/
    const editEvent2 = (eid) => {
      router.push(`/repair/repairManagement/repairOrder/repairOrder-edit/${eid}`);
    }

    /*TODO:编辑关闭事件*/
    const editEventClose = () => {
      console.log(gridOptions.formData);
      VXETable.modal.message({content: '提交成功', status: 'success'})
    }

    /*TODO:查看事件*/
    const viewEvent = (row) => {
      repairOrderListApp.currentId = row.eid;
      repairOrderListApp.viewModalShowing = true
      gridOptions.showDetailsTitle = '查看';
    }

    /*TODO:删除事件*/
    const deleteEvent = (row) => {
      RepairOrderService.deleteRepairOrder(row.eid).then(() => {
        VXETable.modal.message({content: '操作成功', status: 'success'})
      }).catch(error => {
        VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:批量删除*/
    const batchDeleteEvent = () => {
      VXETable.modal.message({content: '点了批量删除', status: 'success'})
    }
    /*TODO:条件查询提交*/
    const reload = () => {
      const $grid = repairOrderGrid.value
      $grid.commitProxy('query')//TODO:执行代理方法
    }
    /*TODO:重置搜索*/
    const reset = () => {
      where.value = {}
      const $grid = repairOrderGrid.value
      $grid.commitProxy('query')//TODO:执行代理方法
    }
    let autoConfig = ref({});
    RepairOrderService.getConfigByName("自动派单").then(res => {
      console.log(res)
      autoConfig.value = res.data
      autoEable.value = res.data.value == '1' ? true : false
    })

    const autoChange = (parma) => {
      console.log(parma)
      autoConfig.value.value = parma ? "1" : "0";
      updateConfig()
    }
    const updateConfig = () => {
      console.log(autoConfig)
      RepairOrderService.updateConfig(autoConfig.value).then(res => {
        console.log(res)
      })
    }
    let dispatchData = reactive({
      repairOrderId: "",
      userId: "",

    });
    const dispatchShow = (id) => {
      console.log(id)
      repairOrderListApp.dispatchModal = true
      dispatchData.repairOrderId = id;
    }
    const dispatchEvent = () => {
      console.log(dispatchData)
      RepairOrderService.dispatch(dispatchData.userId, dispatchData.repairOrderId).then((res) => {
        if (res.data.code == 0) {
          VXETable.modal.message({content: '操作成功', status: 'success'})
          repairOrderListApp.dispatchModal = false
          reload();
        }
      }).catch(error => {
        VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
      })
    }
    const onFinishFailed = (errorInfo) => {
      console.log('Failed:', errorInfo);
    };
    //TODO:这里需要返回才能调用
    return {
      eid,
      repairOrderGrid,
      // editModal,
      gridOptions,
      searchExpand,
      addEvent,
      editEvent,
      viewEvent,
      editEventClose,
      deleteEvent,
      batchDeleteEvent,
      editEvent2,
      addEvent2,
      where,
      reload,
      reset,
      repairOrderListApp,
      ...toRefs(proxyInfo),
      autoEable,
      autoChange,
      dispatchShow,
      dispatchForm,
      dispatchEvent,
      dispatchData,
      onFinishFailed
    }
  }
})
</script>

<style scoped>
.my-grid66 .alert-message {
  height: 40px;
  display: flex;
  align-items: center;
  margin: 10px 0;
  border-radius: 4px;
  background-color: #e6f7ff;
  border: 1px solid #91d5ff;
}

.my-grid66 .alert-message-icon {
  width: 30px;
  text-align: center;
  color: #409eff;
  margin-right: 8px;
}

.my-grid66 .alert-message-content {
  flex-grow: 1;
  padding-right: 20px;
}

.my-grid66 .page-left {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}

.my-grid66 .select-count {
  display: inline-block;
  vertical-align: middle;
}

.my-grid66 .my-input {
  width: 100%;
}

.my-grid66 .first-col {
  position: relative;
  height: 20px;
}

.my-grid66 .first-col:before {
  content: "";
  position: absolute;
  left: -15px;
  top: 10px;
  width: 110px;
  height: 1px;
  transform: rotate(28deg);
  background-color: #e8eaec;
}

.my-grid66 .first-col .first-col-top {
  position: absolute;
  right: 4px;
  top: -10px;
}

.my-grid66 .first-col .first-col-bottom {
  position: absolute;
  left: 4px;
  bottom: -10px;
}
</style>
